/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.FiscalPeriod;
import org.crystalrange.enterprise.service.FiscalPeriodService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class FiscalPeriodListController extends BaseController {

    protected FiscalPeriodService fiscalPeriodService;
    protected FiscalPeriod selectedFiscalPeriod;
    protected Listbox listboxFiscalPeriod;
    protected Search searchFiscalPeriod;
    protected List<FiscalPeriod> fiscalPeriods;

    public FiscalPeriodService getFiscalPeriodService() {
        return this.fiscalPeriodService;
    }

    public void setFiscalPeriodService(FiscalPeriodService fiscalPeriodService) {
        this.fiscalPeriodService = fiscalPeriodService;
    }

    public FiscalPeriod getSelectedFiscalPeriod() {
        return this.selectedFiscalPeriod;
    }

    public void setSelectedFiscalPeriod(FiscalPeriod selectedFiscalPeriod) {
        this.selectedFiscalPeriod = selectedFiscalPeriod;
    }

    public List<FiscalPeriod> getFiscalPeriods() {
        return this.fiscalPeriods;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchFiscalPeriod = new Search(FiscalPeriod.class);
        this.searchFiscalPeriod.setMaxResults(100);
        this.searchFiscalPeriod.addFetch("fiscalPeriodVariant");
        this.searchFiscalPeriod.addFetch("fiscalYear");
        this.searchFiscalPeriod.addFetch("createdBy");
        this.searchFiscalPeriod.addFetch("updatedBy");
        this.fiscalPeriods = this.fiscalPeriodService.search(this.searchFiscalPeriod);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemFiscalPeriodCreate(Event event) throws Exception {
        this.fiscalPeriodCreate();
    }

    public void onClick$menuitemFiscalPeriodEdit(Event event) throws Exception {
        this.fiscalPeriodEdit();
    }

    public void onClick$menuitemFiscalPeriodDelete(Event event) throws Exception {
        this.fiscalPeriodDelete();
    }

    public void onClick$menuitemFiscalPeriodSearch(Event event) throws Exception {
        this.fiscalPeriodSearch();
    }

    public void onClick$menuitemFiscalPeriodRefresh(Event event) throws Exception {
        this.fiscalPeriodRefresh();
    }

    public void onClick$toolbarbuttonFiscalPeriodCreate(Event event) throws Exception {
        this.fiscalPeriodCreate();
    }

    public void onClick$toolbarbuttonFiscalPeriodEdit(Event event) throws Exception {
        this.fiscalPeriodEdit();
    }

    public void onClick$toolbarbuttonFiscalPeriodDelete(Event event) throws Exception {
        this.fiscalPeriodDelete();
    }

    public void onClick$toolbarbuttonFiscalPeriodSearch(Event event) throws Exception {
        this.fiscalPeriodSearch();
    }

    public void onClick$toolbarbuttonFiscalPeriodRefresh(Event event) throws Exception {
        this.fiscalPeriodRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.fiscalPeriodEdit();
    }
    
    private void fiscalPeriodCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/fiscalPeriod.zul");
    }

    private void fiscalPeriodEdit() throws Exception {
        if (this.selectedFiscalPeriod != null) {
            this.execution.sendRedirect("~./zul/html/fiscalPeriod.zul?Id=" + this.selectedFiscalPeriod.getId().toString());
        }
    }

    private void fiscalPeriodDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxFiscalPeriod.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    FiscalPeriod fiscalPeriod = (FiscalPeriod) selectedItem.getValue();
                    this.fiscalPeriodService.remove(fiscalPeriod);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.fiscalPeriods = this.fiscalPeriodService.search(this.searchFiscalPeriod);
        this.binder.loadAll();
    }

    private void fiscalPeriodSearch() throws Exception {
        this.execution.setAttribute("searchFiscalPeriod", this.searchFiscalPeriod);

        Window window = (Window) this.execution.createComponents("~./zul/html/fiscalPeriodSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchFiscalPeriod = (Search) this.execution.getAttribute("searchFiscalPeriod");
        }

        this.fiscalPeriods = this.fiscalPeriodService.search(this.searchFiscalPeriod);
        this.binder.loadAll();
    }

    private void fiscalPeriodRefresh() throws Exception {
        this.fiscalPeriods = this.fiscalPeriodService.search(this.searchFiscalPeriod);
        this.binder.loadAll();
    }
}
